using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;

namespace JsonRpcGen.TypeHandler
{
    internal class EnumTypeHandler : TypeHandler
    {
        private readonly JArray _enums;

        public EnumTypeHandler(JToken enums)
        {
            _enums = (JArray)enums;
        }

        public override void Write(StreamWriter writer, string namesp, string className, Dictionary<string, string> replacements)
        {
            if(namesp==null)
            {
                WriteEnum(writer, className);    
                return;
            }
            writer.WriteLine("namespace " + namesp);
            writer.WriteLine("{");
            WriteEnum(writer, className);
            writer.WriteLine("}");
            EnumTypes.Add(namesp+"."+className);
        }

        public void WriteEnum(StreamWriter writer, string className)
        {

            writer.WriteLine("   public enum " + className);

            writer.WriteLine("   {");
            foreach (var eval in _enums)
            {
                var name = eval.ToString();
                if (Global.IsReservedName(name))
                {
                    writer.WriteLine("       [global::System.Runtime.Serialization.EnumMember(Value=\"" + name + "\")]");
                    name = Global.MakeFirstUpper(name);
                }
                name = name.Replace('.', '_');
                writer.WriteLine("       " + name + ",");
            }
            writer.WriteLine("   }");
        }


        public static List<string> EnumTypes=new List<string>();
    }
}